<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改项目成本预估')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-cbpj-edit" th:object="${gcProjectCbpj}">
        	<h4 class="form-header h4">基本信息</h4>
            <input name="id" th:field="*{id}" type="hidden">
            <input type="hidden" name="gcProjectid" id="gcProjectid" th:value="*{gcProjectid}">
            <div class="form-group">      
                <label class="col-sm-1 control-label">合同编号：</label>
                <div class="col-sm-2">
                    <input name="orderNo" th:field="*{orderNo}" autoComplete="off" class="form-control" type="text" readonly="readonly">
                </div>
                <label class="col-sm-1 control-label is-required">负责人：</label>
                <div class="col-sm-2">
                    <input name="xmfzr" th:field="*{xmfzr}" autoComplete="off" class="form-control" type="text" required>
                </div>
                <label class="col-sm-1 control-label is-required">合同金额：</label>
                <div class="col-sm-2">
                    <input name="htje" id="htje" th:field="*{htje}" autoComplete="off" class="form-control" type="text" required>
                </div>
                <label class="col-sm-1 control-label is-required">评估时间：</label>
                <div class="col-sm-2">
                    <div class="input-group date">
                        <input required name="pgsj" th:value="${#dates.format(gcProjectCbpj.pgsj, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" autoComplete="off" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
            	<label class="col-sm-1 control-label is-required">项目名称：</label>
                <div class="col-sm-5">
                    <input name="xmmc" th:field="*{xmmc}" class="form-control" autoComplete="off" type="text" required>
                </div>
                <label class="col-sm-1 control-label is-required">利润预估：</label>
                <div class="col-sm-2">
                    <input name="lryg" th:field="*{lryg}" class="form-control" autoComplete="off" type="text" required>
                </div>
                <label class="col-sm-1 control-label">成本合计：</label>
                <div class="col-sm-2">
                    <input name="cbhj" th:field="*{cbhj}" class="form-control" autoComplete="off" type="text" readonly="readonly">
                </div>
            </div>
            <h4 class="form-header h4">表单项&nbsp;&nbsp;
            	<button th:if="${gcProjectCbpj.status} eq 0" type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                <button th:if="${gcProjectCbpj.status} eq 0" type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
            </h4>
            <div class="row">
                <div class="col-sm-12">
				    <table id="bootstrap-table"></table>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
       		<button th:if="(${gcProject.status} eq 0) or (${gcProject.status} eq 2) or (${gcProject.status} eq 4)" type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:src="@{/js/math.min.js}"></script>
    <th:block th:include="include :: datetimepicker-js" />
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "gcgl/cbpj"
        var hjje = 0;//项的合计金额
        $("#form-cbpj-edit").validate({
            focusCleanup: true
        });

        $("input[name='pgsj']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        
        $(function() {
	    	// 初始化数据, 可以由后台传过来
	    	var data = [[${gcProjectCbpj.item}]];
	    	
	    	
		    var options = {
		    	data: data,
                pagination: false,
		        showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
		        columns: [
		        {
		        	field: 'index',
		        	align: 'center',
		        	title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	var columnId = $.common.sprintf("<input type='hidden' name='item[%s].xuh' value='%s'>", index, row.id);
                    	return columnIndex + $.table.serialNumber(index) + columnId;
                    }
                },
                {
		            field: 'type',
		            align: 'center',
		            title: '成本类型',
		            width:140,
		            formatter: function(value, row, index) {
		        		var data = [{ index: index, type: value }];
		                return $("#itemTypeTpl").tmpl(data).html();
                    }
		        },
		        {
		            field: 'name',
		            align: 'center',
		            title: '名称及类型',
		            width:230,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input autoComplete='off' class='form-control itemname' type='text' name='item[%s].name' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'xinghao',
		            align: 'center',
		            title: '型号',
		            width:270,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input autoComplete='off' class='form-control itemxinghao' type='text' name='item[%s].xinghao' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'count',
		            align: 'center',
		            title: '数量',
		            width:80,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input autoComplete='off' class='form-control itemcount' type='text' name='item[%s].count' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'dw',
		            align: 'center',
		            title: '单位',
		            width:80,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input autoComplete='off' class='form-control itemdw' type='text' name='item[%s].dw' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'suilv',
		            align: 'center',
		            title: '税率',
		            width:80,
		            formatter: function(value, row, index) {
		        		var data = [{ index: index, suilv: value+"" }];
		        		
		        		console.log("data---"+value);
		                return $("#itemSuilv").tmpl(data).html();
                    }
		        },
		        {
		            field: 'cbdj',
		            align: 'center',
		            title: '成本单价',
		            width:110,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input autoComplete='off' class='form-control itemcbdj' type='text' name='item[%s].cbdj' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'xjjg',
		            align: 'center',
		            title: '小计价格',
		            width:150,
		            formatter: function(value, row, index) {
// 		            	var html = $.common.sprintf("<input class='itemxjjg' type='hidden' name='item[%s].xjjg' value='%s'>", index, value);
// 		            	return html + '<span class="js-total-price">' + value + '</span>';
		            	
		            	var html = $.common.sprintf("<input autoComplete='off' class='form-control itemxjjg' type='text' name='item[%s].xjjg' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'jxsj',
		            align: 'center',
		            title: '进项税金',
		            width:110,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input autoComplete='off' class='form-control itemjxsj' type='text' name='item[%s].jxsj' value='%s'>", index, value);
		            	return html;
                    }
		        },
		        {
		            field: 'memo',
		            align: 'center',
		            title: '备注',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input autoComplete='off' class='form-control' type='text' name='item[%s].memo' value='%s'>", index, value);
		        		return html;
                    }
		        },
		        {
                    title: '操作',
                    align: 'center',
                    width:70,
                    formatter: function(value, row, index) {
                    	var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
		    };
		    $.table.init(options);
		    
		 	// 监听表格内输入框值的变化
	        $("#bootstrap-table").on("change", "td", e => {
	        	hjje=0;
	      	  let target = $(e.target);
	      	  let currentTr = target.closest("tr");
	      	  let el_count = currentTr.find(".itemcount"); // 数量元素
	      	  let el_price = currentTr.find(".itemcbdj"); // 成本单价元素
	      	  let el_suilv = currentTr.find(".itemsuilv"); // 税率元素
	      	  
	      	  // 使用math.js解决js在数学计算时出现的精度问题
	      	  // 计算公式：
	      	  // 小计价格=数量x单价
	      	  let subtotal = math.format(
	      	  	math.multiply(math.bignumber(el_count.val()), math.bignumber(el_price.val()))
	      	  );
	      	  currentTr.find(".itemxjjg").val(Math.round(subtotal * 100) / 100).next(".js-total-price").text(Math.round(subtotal * 100) / 100);
      			
	      	  hjje = math.add(hjje,subtotal);
	      	  
	      	  // 进项税金=小计价格/([1+税率]*税率)
	      	  let jxsjPrice = math.format(
	      	  	math.multiply(math.divide(math.bignumber(subtotal),math.add(1,math.divide(el_suilv.val(),100))),math.divide(el_suilv.val(),100))
	      	  );
	      	  currentTr.find(".itemjxsj").val(Math.round(jxsjPrice * 100) / 100);
	      	  
	      	  updateTotal();
	        });
		});
        
     	// 更新最后金额
        function updateTotal(){
      	  var cbhj = 0;
      	  $("#bootstrap-table").find("tbody tr").each((index, element) => {
      		  cbhj = math.format(
    			 math.add(math.bignumber(cbhj), math.bignumber($(element).find(".itemxjjg").val()))
    		  );
      	  });
      	  
      	  $("#cbhj").val(Math.round(cbhj * 100) / 100);
        }
        
        $.validator.addMethod("itemtype", function(value, element) {
            return !this.optional(element);
        }, "成本类型必填。");
        $.validator.addMethod("itemname", function(value, element) {
            return !this.optional(element);
        }, "名称及类型必填。");
        $.validator.addMethod("itemxinghao", function(value, element) {
            return !this.optional(element);
        }, "型号必填。");
        $.validator.addMethod("itemcount", function(value, element) {
            return !this.optional(element) && (value <= 10000000 && value >= 1);
        }, "数量长度区间1-10000000。");
        $.validator.addMethod("itemsuilv", function(value, element) {
            return !this.optional(element);
        }, "税率必填。");
        $.validator.addMethod("itemjxsj", function(value, element) {
            return !this.optional(element) && (value <= 10000000 && value >= 0);
        }, "进项税金长度区间0-10000000。");
        $.validator.addMethod("itemxjjg", function(value, element) {
            return !this.optional(element) && (value <= 10000000 && value >= 1);
        }, "小计价格长度区间1-10000000。");
        $.validator.addMethod("itemcbdj", function(value, element) {
            return !this.optional(element) && (value <= 10000000 && value >= 1);
        }, "成本单价长度区间1-10000000。");
        
        function submitHandler(index, layero) {
            if ($.validate.form()) {
            	var data = $("#form-cbpj-edit").serializeArray();
                $.operate.saveTab("/gcgl/cbpj/edit", data);
            }
        }
        
        function addRow() {
        	var count = $("#" + table.options.id).bootstrapTable('getData').length;
        	var row = {
                index: $.table.serialNumber(count),
                type: "",
                name: "",
                xinghao: "",
                count: "",
                suilv: "",
                cbdj: "",
                jxsj: "",
                xjjg: "",
                memo: "",
                dw:"",
            }
        	sub.addRow(row);
        }
    </script>
</body>
</html>

<!-- 成本类型 -->
<script id="itemTypeTpl" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='item[${index}].type'>
    <option value="设备/材料" {{if type==="设备/材料"}}selected{{/if}}>设备/材料</option>
	<option value="机械/租赁费" {{if type==="机械/租赁费"}}selected{{/if}}>机械/租赁费</option>
<option value="施工工程费" {{if type==="施工工程费"}}selected{{/if}}>施工工程费</option>
	<option value="工程其它费用" {{if type==="工程其它费用"}}selected{{/if}}>工程其它费用</option>
	<option value="一般性费用" {{if type==="一般性费用"}}selected{{/if}}>一般性费用</option>
	<option value="工程管理费" {{if type==="工程管理费"}}selected{{/if}}>管理费</option>
<option value="所有税金" {{if type==="所有税金"}}selected{{/if}}>所有税金</option>
</select>
</div>
</script>

<!-- 成本类型 -->
<script id="itemSuilv" type="text/x-jquery-tmpl">
<div>
<select class='form-control itemsuilv' name='item[${index}].suilv'>
    <option value="9" {{if suilv==="9"}}selected{{/if}}>9%</option>
	<option value="6" {{if suilv==="6"}}selected{{/if}}>6%</option>
	<option value="3" {{if suilv==="3"}}selected{{/if}}>3%</option>
	<option value="1" {{if suilv==="1"}}selected{{/if}}>1%</option>
	<option value="0" {{if suilv==="0"}}selected{{/if}}>0%</option>
	<option value="11" {{if suilv==="11"}}selected{{/if}}>11%</option>
	<option value="13" {{if suilv==="13"}}selected{{/if}}>13%</option>
</select>
</div>
</script>